package com.jiantong.work.impl.manager;

import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.impl.dao.BreedFactoryDao;
import com.jiantong.user.model.BreedFactory;
import com.jiantong.work.enums.DocumentsType;
import com.jiantong.work.impl.dao.ReportHandleResumeDao;
import com.jiantong.work.impl.dao.RetailBreedRecordDao;
import com.jiantong.work.model.ReportHandleResume;
import com.jiantong.work.model.RetailBreedRecord;
import com.jiantong.work.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by mz on 25/7/17.
 *
 */
@Component
public class RetailBreedRecordManager {

    private final RetailBreedRecordDao retailBreedRecordDao;

    private final BreedFactoryDao breedFactoryDao;

    private final ReportHandleResumeDao reportHandleResumeDao;

    @Autowired
    public RetailBreedRecordManager(RetailBreedRecordDao retailBreedRecordDao,
                                    BreedFactoryDao breedFactoryDao,
                                    ReportHandleResumeDao reportHandleResumeDao) {
        this.retailBreedRecordDao = retailBreedRecordDao;
        this.breedFactoryDao = breedFactoryDao;
        this.reportHandleResumeDao = reportHandleResumeDao;
    }

    @Transactional
    public Long create(RetailBreedRecord retailBreedRecord , Integer toStatus){
        Integer fromStatus = retailBreedRecord.getStatus();
        Long id = null;

        LoginUser loginUser = UserUtil.getCurrentUser();
        String prefix = "RSC";
        prefix = Utils.createPrefix(prefix,loginUser);
        String number = Utils.create(prefix,retailBreedRecordDao.findCurrentMaxNumber(prefix));
        retailBreedRecord.setNumber(number);

        if(toStatus != null && !fromStatus.equals(toStatus)){
            retailBreedRecord.setStatus(toStatus);
            retailBreedRecordDao.create(retailBreedRecord);
            id = retailBreedRecord.getId();

            ReportHandleResume reportHandleResume = new ReportHandleResume();
            reportHandleResume.setReportId(id);
            reportHandleResume.setReportNumber(number);
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);
            reportHandleResume.setUserId(loginUser.getId());
            reportHandleResume.setUserName(loginUser.getName());
            reportHandleResume.setType(DocumentsType.RETAIL_BREED_RECORD.value());

            reportHandleResumeDao.create(reportHandleResume);

            //todo how to fix
            BreedFactory breedFactory = new BreedFactory();
            breedFactory.setId(retailBreedRecord.getBreedFactoryId());
            breedFactory.setStockQuantity(retailBreedRecord.getStock());
            breedFactoryDao.update(breedFactory);

        }else {
            retailBreedRecordDao.create(retailBreedRecord);
            id = retailBreedRecord.getId();
        }

        return id;
    }

    @Transactional
    public Boolean update(RetailBreedRecord retailBreedRecord , Integer toStatus){
        Integer fromStatus = retailBreedRecord.getStatus();
        Boolean flag = false;
        if(toStatus != null && !fromStatus.equals(toStatus)){
            retailBreedRecord.setStatus(toStatus);
            flag = retailBreedRecordDao.update(retailBreedRecord);

            ReportHandleResume reportHandleResume = new ReportHandleResume();

            reportHandleResume.setReportId(retailBreedRecord.getId());
            reportHandleResume.setReportNumber(retailBreedRecord.getNumber());
            reportHandleResume.setUserId(UserUtil.getCurrentUser().getId());
            reportHandleResume.setUserName(UserUtil.getCurrentUser().getName());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);
            reportHandleResume.setType(DocumentsType.RETAIL_BREED_RECORD.value());

            reportHandleResumeDao.create(reportHandleResume);

            //todo how to fix
            BreedFactory breedFactory = new BreedFactory();
            breedFactory.setId(retailBreedRecord.getBreedFactoryId());
            breedFactory.setStockQuantity(retailBreedRecord.getStock());
            breedFactoryDao.update(breedFactory);
        }else {
            flag = retailBreedRecordDao.update(retailBreedRecord);
        }
        return flag;
    }


}
